<?php

namespace App\Console\Commands;

use App\Export;
use App\Lead;
use App\Project;
use App\Record;
use Carbon\Carbon;
use http\Client;
use Illuminate\Console\Command;
use mysql_xdevapi\Result;

class ExportCustomer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'export:customer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $lead = \App\Lead::where('create_time', '>=', strtotime('2022-03-05 00:00:00'))
        ->whereNotNull('project_id')
            ->where('create_user_id','!=',1)
        ;
        $bar = $this->output->createProgressBar($lead->count());
        $bar->start();
        $lead->chunkById(100, function ($items)use ($bar) {
            foreach ($items as $item) {
                $bar->advance();
                /**
                 * @var Lead $item
                 */
                if (Export::where('lead_id', $item)->first()) {
                    continue;
                }
                $export = new Export();
                $export->lead_id=$item->leads_id;
                $export->time = Carbon::parse($item->create_time)->toDateTimeString();
                try {
                    $export->project_name = Project::find($item->project_id)->name ?? '';
                } catch (\Exception $exception) {

                }
                $export->name = $item->name;
                $export->phone = $item->mobile;
                $export->follow = Record::orWhere((function ($query) use ($item) {
                    $query->where('types', 'crm_customer')
                        ->where('types_id', $item->customer_id);
                }))->orWhere((function ($query) use ($item) {
                    $query->where('types', 'crm_leads')
                        ->where('types_id', $item->leads_id);
                }))->orderByDesc('create_time')
                    ->get()->implode('content', ',');
                $client = new \GuzzleHttp\Client();
                $result = $client->get('https://cx.shouji.360.cn/phonearea.php', [
                    'query' => [
                        'number' => $item->mobile
                    ]
                ]);
                $result = json_decode($result->getBody()->getContents(), true);
                if (isset($result['code']) && $result['code'] == 0) {
                    $export->provice = $result['data']['province'];
                    $export->city = $result['data']['city'];
                    $export->type = $result['data']['sp'];
                }
                $export->save();

            }
        });
            $bar->finish();
    }
}
